﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

public partial class backstage_customer_service : System.Web.UI.Page
{
    // Fields

    protected CustomerService cs = new CustomerService();
    protected CustomerServiceManager csm = new CustomerServiceManager();
    protected string xmlPath = "/cn/xml/customer_service.xml";

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.xmlPath));
            XmlElement element = (XmlElement)document.SelectSingleNode("CustomerService");
            element.SetAttribute("title", this.txtCustomerServiceTitle.Value);
            element.SetAttribute("isShow", this.rdIsState1.Checked ? "true" : "false");
            document.Save(base.Server.MapPath(this.xmlPath));
            DataTable customerServiceByLanguageVersion = this.csm.GetCustomerServiceByLanguageVersion(this.Session["language_version"].ToString());
            ArrayList list = new ArrayList();
            if (base.Request.Form["hdCustomerService"] != null)
            {
                list = ArrayList.Adapter(base.Request.Form["hdCustomerService"].Split(new char[] { ',' }));
            }
            foreach (DataRow row in customerServiceByLanguageVersion.Rows)
            {
                if (!list.Contains(row["id"].ToString()))
                {
                    this.csm.DeleteCustomerService(Convert.ToInt32(row["id"]));
                }
            }
            if (base.Request.Form["customerServiceType"] != null)
            {
                ArrayList list2 = ArrayList.Adapter(base.Request.Form["customerServiceType"].Split(new char[] { ',' }));
                ArrayList list3 = ArrayList.Adapter(base.Request.Form["title"].Split(new char[] { ',' }));
                ArrayList list4 = ArrayList.Adapter(base.Request.Form["customerServiceNo"].Split(new char[] { ',' }));
                this.cs.LanguageVersion = this.Session["language_version"].ToString();
                for (int i = 0; i < list2.Count; i++)
                {
                    this.cs.CustomerServiceType = Convert.ToInt32(list2[i]);
                    this.cs.CustomerServiceNo = list4[i].ToString();
                    this.cs.Title = list3[i].ToString();
                    this.cs.Id = Convert.ToInt32(list[i]);
                    if (this.cs.Id != 0)
                    {
                        this.csm.ModifyCustomerService(this.cs);
                    }
                    else
                    {
                        this.csm.AddCustomerService(this.cs);
                    }
                }
            }
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"customer_service.aspx\";</script>");
        }
        catch
        {
        }
    }

    private void CustomerServiceBind()
    {
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.xmlPath));
            XmlElement element = (XmlElement)document.SelectSingleNode("CustomerService");
            this.txtCustomerServiceTitle.Value = element.GetAttribute("title");
            this.rdIsState1.Checked = element.GetAttribute("isShow") == "true";
            this.rdIsState0.Checked = element.GetAttribute("isShow") != "true";
        }
        catch
        {
        }
    }

    protected StringBuilder GetCustomerService()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (this.Session["language_version"] == null)
            {
                return builder;
            }
            DataTable customerServiceByLanguageVersion = this.csm.GetCustomerServiceByLanguageVersion(this.Session["language_version"].ToString());
            builder.AppendLine("<table border=\"0\" cellspacing=\"0\" id=\"CustomerService\">");
            foreach (DataRow row in customerServiceByLanguageVersion.Rows)
            {
                string str = "";
                switch (Convert.ToInt32(row["customerServiceType"]))
                {
                    case 0:
                        str = "QQ：";
                        break;

                    case 1:
                        str = "MSN：";
                        break;

                    case 2:
                        str = "旺旺：";
                        break;

                    case 3:
                        str = "Skype：";
                        break;
                }
                builder.AppendLine(string.Concat(new object[] { "<tr><td>", str, "<input name=\"hdCustomerService\" type=\"hidden\" value=\"", row["id"], "\" /><input name=\"customerServiceType\" type=\"hidden\" value=\"", row["customerServiceType"], "\" /></td><td><input name=\"customerServiceNo\" type=\"text\" value=\"", row["customerServiceNo"], "\" /> 标题：<input name=\"title\" type=\"text\" value=\"", row["title"], "\" /><input type=\"button\" class=\"formButton\" value=\"-\" title=\"删除客服\" onclick=\"delCustomerService(this.parentNode.parentNode.rowIndex)\"></td></tr>" }));
            }
            builder.AppendLine("</table>");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack)
        {
            this.CustomerServiceBind();
        }
    }

}